<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/bianliang.js"></script>
    <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>

    <script>
        $(document).ready(function () {
            var openId;

            $.getJSON("/map/getConfig/", function (json) {
                wx.config({
                    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                    appId: 'wxd230d51f27b1c93a', // 必填，公众号的唯一标识
                    timestamp: json.timeStamp, // 必填，生成签名的时间戳
                    nonceStr: json.nonceStr, // 必填，生成签名的随机串
                    signature: json.signature,// 必填，签名
                    jsApiList: [
                        'openLocation',
                        'getLocation'
                    ] // 必填，需要使用的JS接口列表，所有JS接口列表见官方文档附录2
                });
                wx.ready(function () {
                    console.log("this is ready");
                    getLocation();
                });
                wx.error(function (res) {
                    console.log("this is error");
                });
            });

            $.getJSON("/map/getOpenId/", function (json) {
                openId = json.openId;
                var user = {
                    openId: openId
                };
                $.ajax({
                    type: "get",
                    url: urlUserCenter + "/user/getUserByOpenId",
                    data: user,
                    dataType: "json",
                    success: function (data) {
                        if (data[0] == "" || data[0] == null) {
                            return;
                        }
                        else {
                            alert("该微信号已经绑定过自在天原账号！");
                        }
                    }
                })
            });

            $("#b_submit").click(function () {
                var username = $("#userName").val();
                var password = $("#password").val();
                var user = {
                    name: username,
                    password: password,
                    openId: openId
                };
                $.ajax({
                    async: false,   //是否为异步请求
                    cache: false,  //是否缓存结果
                    type: "post",
                    url: urlUserCenter + "/user/checkUser",
                    data: user,
                    success: function (data) {
                        if ("false1" == data.result) {
                            alert("用户名不存在！");
                            return;
                        }
                        if ("false2" == data.result) {
                            alert("用户名与密码不匹配！");
                            return;
                        }
                        if ("success" == data.result) {
                            updateOpenIdToUser(user);
                            addAbility(user);
                        }
                    },
                    error: function (err) {
                        console.log(err);
                    }
                });
            });

            function updateOpenIdToUser(data) {
                $.ajax({
                    async: false,   //是否为异步请求
                    cache: false,  //是否缓存结果
                    type: "post",
                    url: urlUserCenter + "/user/updateOpenIdToUser",
                    data: data,
                    dataType: "json",
                    success: function (data) {
                        if ("success" == data.result) {
                            alert("绑定成功！");
                        }
                        if ("false" == data.result) {
                            alert("绑定失败！");
                        }
                    },
                    error: function (err) {
                        console.log(err);
                    }
                });
            }

            function addAbility(data) {
                $.ajax({
                    type: "get",
                    url: "/ability/addAbilityByUserId",
                    data: data,
                    dataType: "json",
                    success: function (data1) {
                        console.log(data1);
                    },
                    error: function (err) {
                        console.log(err);
                    }
                });
            }

        });
    </script>
</head>
<body>


<div>
    <div id="formbackground" style="position:absolute; width:100%; height:100%; z-index:-1">
        <img src="../img/user.png" height="100%" width="100%"/>
    </div>
    <div class="main">

        <h1>绑定公众号</h1>
        <div style="text-align:center">

            <table>
                <tr>
                    <h1 class="pagetitle">
                        <td>用户名：</td>
                        <td><input type="text" id="userName"/></td>
                    </h1>
                </tr>
                <tr>
                    <h1 class="pagetitle">
                        <td>密码：</td>
                        <td><input type="text" id="password"/></td>
                    </h1>
                </tr>
            </table>

            <input type="button" id="b_submit" value="确认"/>
            <input type="button" onClick="window.close();" value="取消"/>

        </div>

    </div>
</div>

</body>

<style>
    .main{
        text-align: center; /*让div内部文字居中*/
        border-radius: 20px;
        width: 300px;
        height: 350px;
        margin: auto;
        position: absolute;
        top: 30%;
        left: 0;
        right: 0;
        bottom: 0;
    }
    td
    {
        text-align:center;
    }


</style>

</html>